package com.lgcns.board;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;

import com.lgcns.mybatis.BoardMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class BoardDao {
	
	private static BoardDao instance = new BoardDao();
	
	private String resource = "com/lgcns/mybatis/Configuration.xml";
	private Reader reader;
	private SqlSessionFactory sqlSessionFactory;
	
	public static BoardDao getInstance()
	{
		return instance;
	}
	
	private BoardDao()
	{
		try
		{
			reader = Resources.getResourceAsReader(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
		} catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)
	{
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public ArrayList<Article> getArticleList(HashMap<String, String> hashmap)
	{
		ArrayList<Article> articleList = new ArrayList<Article>();
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			articleList = mapper.getArticleList(hashmap);
		} finally
		{
			session.close();
		}

		return articleList;
	}
	
	public int getTotalRecord(HashMap<String, String> hashmap)
	{
		int totalRecord = 0;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			totalRecord = mapper.getTotalRecord(hashmap);
		} finally
		{
			session.close();
		}

		return totalRecord;
	}
	
	public int insert(Article article)
	{
		SqlSession session = sqlSessionFactory.openSession();
		int ret = -1;
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			ret = mapper.insert(article);
			session.commit();
		} finally
		{
			session.close();
		}
		
		return ret;
	}
	
	public AttachFile getAttachFile(int attachFileNo)
	{
		AttachFile attachFile = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			attachFile = mapper.getAttachFile(attachFileNo);
		} finally
		{
			session.close();
		}
		
		return attachFile;
	}
	
	public void insertAttachFile(AttachFile attachFile)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.insertAttachFile(attachFile);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public void update(Article article)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.update(article);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public void delete(int articleNo)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.delete(articleNo);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public void increseHit(int articleNo)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.increaseHit(articleNo);
			session.commit();
		} finally
		{
			session.close();
		}
		
	}
	
	public Article getArticle(int articleNo)
	{
		Article article = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			article = mapper.getArticle(articleNo);
		} finally
		{
			session.close();
		}
		
		return article;
	}
	
	public Article getNextArticle(HashMap<String, String> hashmap)
	{
		Article article = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			article = mapper.getNextArticle(hashmap);
		} finally
		{
			session.close();
		}
		
		return article;
	}
	
	public Article getPrevArticle(HashMap<String, String> hashmap)
	{
		Article article = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			article = mapper.getPrevArticle(hashmap);
		} finally
		{
			session.close();
		}
		
		return article;
	}
	
	public ArrayList<AttachFile> getAttachFileList(int articleNo)
	{
		ArrayList<AttachFile> attachFileList = new ArrayList<AttachFile>();
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			attachFileList = mapper.getAttachFileList(articleNo);
		} finally
		{
			session.close();
		}
		
		return attachFileList;
	}
	
	public void deleteFile(int attachFileNo)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.deleteFile(attachFileNo);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public String getBoardNm(String boardCd)
	{
		String boardNm = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			boardNm = mapper.getBoardNm(boardCd);
		} finally
		{
			session.close();
		}
		
		return boardNm;
	}
	
	public ArrayList<Board> getBoardList()	
	{
		ArrayList<Board> boardList = new ArrayList<Board>();
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			boardList = mapper.getBoardList();
		} finally
		{
			session.close();
		}
		
		return boardList;
	}
	
	public void insertComment(Comment comment)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.insertComment(comment);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public void updateComment(Comment comment)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.updateComment(comment);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public void deleteComment(int commentNo)
	{
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			mapper.deleteComment(commentNo);
			session.commit();
		} finally
		{
			session.close();
		}
	}
	
	public Comment getComment(int commentNo)
	{
		Comment comment = null;
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			comment = mapper.getComment(commentNo);
		} finally
		{
			session.close();
		}
		
		return comment;
	}
	
	public ArrayList<Comment> getCommentList(int articleNo)
	{
		ArrayList<Comment> commentList = new ArrayList<Comment>();
		SqlSession session = sqlSessionFactory.openSession();
		
		try
		{
			BoardMapper mapper = session.getMapper(BoardMapper.class);
			commentList = mapper.getCommentList(articleNo);
		} finally
		{
			session.close();
		}
		
		return commentList;
	}

}
